/**
 * 给枚举添加‘全部’数据项
 * @param {*} options 枚举数据
 * @returns 
 */
const getEnumAllOptions = (options) => {
  return [{ label: '全部', value: '' }].concat(options);
}
/**
 * 获取枚举name
 * @param {*} options 枚举数据
 * @param {*} value 枚举值
 * @returns 
 */
const getEnumName = (value, options) => {
  const option = options.find(option => option.value === value);
  return option ? option.label : '';
}

/**
 * 深度拷贝
 * @param {*} data 数据源
 * @returns 拷贝数据
 */
const getDeepCopy = (data) => {
  return JSON.parse(JSON.stringify(data));
}

/**
 * 
 * @param {*} date 日期
 * @returns yyyy-MM-dd格式日期
 */
 const getFormatMonth = (date) => {
  const newDate = date ? new Date(date) : new Date();
  const year = newDate.getFullYear();
  const mouth = (newDate.getMonth() + 1).toString().padStart(2, '0');

  return `${year}-${mouth}`;
}
/**
 * 
 * @param {*} date 日期
 * @returns yyyy-MM-dd格式日期
 */
const getFormatDate = (date) => {
  const newDate = date ? new Date(date) : new Date();
  const year = newDate.getFullYear();
  const mouth = (newDate.getMonth() + 1).toString().padStart(2, '0');
  const day = (newDate.getDate()).toString().padStart(2, '0');

  return `${year}-${mouth}-${day}`;
}

/**
 * 获取当前周 xxxx年xx月第x周（xx月xx日 - xx月xx日）
 */
const getCurrentWeek = () => {
  const todayDate = new Date();
  const curDate = new Date();
  const year = curDate.getFullYear();
  let month = curDate.getMonth() + 1;
  let todayWeekMonth = todayDate.getMonth() + 1;
  // 本周一的日期
  if(curDate.getDay() === 0) {
    curDate.setDate(curDate.getDate() - 6);
  }else {
    curDate.setDate(curDate.getDate() - curDate.getDay() + 1);
  }
  var beginDate = curDate.getDate().toString().padStart(2, '0');
  // 本周日的日期
  curDate.setDate(curDate.getDate() + 6);
  var endDate = curDate.getDate().toString().padStart(2, '0');
  let startMonth = month;
  let endMonth = month;
  if(endDate < beginDate){//结束日期小于开始日期（跨月）
    if(todayDate.getDate() < endDate) {//今天日期小于结束日期，则开始日期为上月（本月 - 1）
      startMonth -= 1;
    } else if(todayDate.getDate() > endDate) { //今天日期大于结束日期，则结束日期都为下月（本月 + 1）
      endMonth += 1;
      todayWeekMonth = endMonth;
    }
  }
  let week = getMonthWeek(year, `${ todayWeekMonth }`, todayDate.getDate()).week;
  let weekCurrentMonth = endDate < beginDate ? endMonth : startMonth;
  return `${year}年${weekCurrentMonth}月第${week}周（${startMonth}月${beginDate}日 - ${endMonth}月${endDate}日）`
  // return `${year}年${mouth}月第${week}周`
}
/**
 * 获取传入年、月、日是当月的第几周
 * @param {*} year 年
 * @param {*} mouth 月
 * @param {*} date 日
 */
const getMonthWeek = (year, mouth, date) => {
  const newDate = new Date(year, parseInt(mouth) - 1, date);
  let w = newDate.getDay();
  let d = newDate.getDate();
  if(w === 0) {
    w = 7;
  }
  let config = {
    year: newDate.getFullYear(),
    month: newDate.getMonth() + 1,
    week: Math.ceil((d + 6 - w) / 7)
  }
  return config
}
/**
 * 获取当前月份天数
 * @returns 当前月份天数
 */
const getCurrentMonthDayNum = () => {
  const date = new Date();
  const year = date.getFullYear();
  const mouth = date.getMonth() + 1;
  const dayNum = new Date(year, mouth, 0).getDate();
  return dayNum;
}
/**
 * 
 * @returns 当前月份第一天
 */
const getMonthFirstDay = () => {
  var y = new Date().getFullYear(); //获取年份
  var m = new Date().getMonth() + 1; //获取月份
  var d = '01'
  m = m < 10 ? '0' + m : m; //月份补 0
  
  return [y,m,d].join("-")
}
/**
 * 
 * @returns 当前月份最后一天
 */
 const getMonthLastDay = () => {
  var y = new Date().getFullYear(); //获取年份
  var m = new Date().getMonth() + 1; //获取月份
  var d = new Date(y, m, 0).getDate(); //获取当月最后一日
  m = m < 10 ? '0' + m : m; //月份补 0
  d = d < 10 ? '0' + d : d; //日数补 0
  return [y,m,d].join("-")
}

/**
 * 
 * @param {*} date 日期
 * @param {*} target 开始、结束
 * @returns 
 */
const getFormatDateTime = (date, target = 'start') => {
  const newDate = date ? new Date(date) : new Date();
  const year = newDate.getFullYear();
  const mouth = (newDate.getMonth() + 1).toString().padStart(2, '0');
  const day = (newDate.getDate()).toString().padStart(2, '0');
  const targetTime = target === 'start' ? '00:00:00' : '23:59:59';
  return `${year}-${mouth}-${day} ${targetTime}`;
}

/**
 * // 防抖
 * @param {*} fn     执行函数
 * @param {*} wait  间隔时间
 * @returns 
 */
const debounce = function(fn, wait) {
  let timer;
  return function () {
    let _this = this;
    let args = arguments;
    if(timer) clearTimeout(timer);
    timer = setTimeout(() => {
      fn.apply(_this, args);
    }, (wait || 200))
  }
}
/**
 * 
 * @param {*} treeMenus children 数组转‘’
 */
const getFormatMenusTreeChildren = (treeMenus) => {
  treeMenus.forEach(menu => {
    if(menu.children && menu.children.length) {
      getFormatMenusTreeChildren(menu.children);
    } else {
      menu.children = '';
    }
  })
}
/**
 * 
 * @param {*} params 参数
 * @returns 
 */
const getFilterEnteryValue = (params) => {
  let data = {};
  Object.keys(params).forEach(key => {
    if(params[key]) {
      data[key] = params[key];
    }
  })
  return data;
}
/**
 * 
 * @param {*} blobData  bolb数据
 * @param {*} fileName  导出文件名称
 */
const downloadBlobData = (blobData, fileName) => {
  const blob = new Blob([blobData], {
    type: 'application/vnd.ms-excel;charset=utf-8'
  })
  // const fileName = `海丰业务综合报表${getFormatDate(queryParams.value.startTime)}`;
  if ('download' in document.createElement('a')) {
    const elink = document.createElement('a')
    elink.download = fileName
    elink.style.display = 'none'
    elink.href = URL.createObjectURL(blob)
    document.body.appendChild(elink)
    elink.click()
    URL.revokeObjectURL(elink.href) // 释放URL 对象
    document.body.removeChild(elink)
  } else {
    // IE10+下载
    navigator.msSaveBlob(blob, fileName)
  }
}
/**
 * 
 * @param {*} url oss地址
 * @param {*} fileName 文件名称
 */
const downloadUrlFile = (url, fileName) => {
  const a = document.createElement('a')
  // 这里是将url转成blob地址，
  fetch(url).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
    a.href = URL.createObjectURL(blob)
    a.download = fileName;
    document.body.appendChild(a)
    a.click()
  })
}
export {
  getEnumAllOptions,
  getEnumName,
  getDeepCopy,
  getFormatMonth,
  getFormatDate,
  getCurrentWeek,
  getCurrentMonthDayNum,
  getMonthFirstDay,
  getMonthLastDay,
  getFormatDateTime,
  debounce,
  getFormatMenusTreeChildren,
  getFilterEnteryValue,
  downloadBlobData,
  downloadUrlFile
  
}